#ifndef LONGKEY_BASE_DATA_POOL_H_
#define LONGKEY_BASE_DATA_POOL_H_

template<typename T>
class DataPool
{
public:
	DataPool(): item_count_(0), head_(NULL), tail_(NULL)
	{
		Initialize();
	}

	virtual ~DataPool()
	{
		Finalization();

		Clear();
	}

	void PushBack(T item)
	{
		PDataPoolItem dpi = new DataPoolItem();
		dpi->item = item;
		dpi->next = NULL;

		if (!head_) head_ = dpi;
		if (tail_) tail_->next = dpi;
		tail_ = dpi;
		item_count_++;
	}

	T PopHead()
	{
		PDataPoolItem dpi = head_;
		if (!dpi) return NULL;

		head_ = dpi->next;
		if (!head_) tail_ = NULL;
		item_count_--;
		T p = dpi->item;
		delete dpi;
		return p;
	}

	int GetItemCount() const { return item_count_; }
	bool Empty() const { return item_count_ == 0; }

protected:
	virtual void Initialize() = 0;
	virtual void Finalization() = 0;

	virtual void Clear()
	{
		while (!Empty())
			PopHead();
	}
private:
	typedef struct DataPoolItem 
	{
		T   item;
		DataPoolItem* next;
	} DataPoolItem, *PDataPoolItem;

	PDataPoolItem head_;
	PDataPoolItem tail_;
	int item_count_;
};


#endif	// LONGKEY_BASE_DATA_POOL_H_